#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "freertos/queue.h"
#include "AS608.h"
#include "Linklist.h"
#include "JQ8900.h"
#include "uart_cmd.h"
#include "RTC.h"
#include "esp_spiffs.h"
#include "spiffs.h"
#include "wifi.h"

SearchResult_t SearchResult;
Infomation_t Pernson_Information;
uint16_t ID[32] = {0};
uint8_t card_model = 0;
Card_clock_t card_clock_config[3];
char Input_name[20];
time_t now_get;  // 定义一个time_t变量now_get
struct tm timeinfo_get; // 定义一个tm结构体变量timeinfo_get
uint8_t now_vol = 5;
uint8_t id_num = 1;
uint8_t page_init[] = {0x70,0x61,0x67,0x65,0x20,0x6D,0x61,0x69,0x6E,0xFF,0xFF,0xFF};

esp_vfs_spiffs_conf_t conf = {
      .base_path = "/spiffs",   //可以认为挂着点，后续使用C库函数fopen("/spiffs/...")
      .partition_label = NULL,  //指定spiffs分区，如果为NULL，则默认为分区表中第一个spiffs类型的分区
      .max_files = 5,           //最大可同时打开的文件数
      .format_if_mount_failed = true
    };

void sort_arr(uint16_t *arr)
{
    size_t size = 32;
    uint16_t first = arr[0];  // 保存第一个元素
    uint16_t temp[31];        // 临时数组，用于保存不为0的元素 (最多31个)
    uint8_t temp_size = 0;

    uint8_t i;
    for (i = 1; i < size; i++) 
    {
        if (arr[i] != 0) 
        {
            temp[temp_size++] = arr[i];
        }
    }

    uint8_t j;
    for (i = 0; i < temp_size - 1; i++) 
    {
        for (j = i + 1; j < temp_size; j++) 
        {
            if (temp[i] > temp[j]) 
            {
                uint16_t swap = temp[i];
                temp[i] = temp[j];
                temp[j] = swap;
            }
        }
    }

    uint8_t j_idx = 0;
    for (i = 1; i < size; i++) 
    {
        if (arr[i] != 0) 
        {
            arr[i] = temp[j_idx++];
        }
    }
}

void GPIO_Test()
{
    gpio_config_t LED_Config;
    LED_Config.pin_bit_mask = (1<<LED_GPIO);
    LED_Config.mode = GPIO_MODE_OUTPUT;
    LED_Config.pull_up_en = GPIO_PULLUP_DISABLE;
    LED_Config.pull_down_en = GPIO_PULLDOWN_DISABLE;
    LED_Config.intr_type = GPIO_INTR_DISABLE;
    gpio_config(&LED_Config);
}

void app_main(void)
{
    uint8_t flag = 0;
    User_UART0_Init(9600);
    User_Uart1_Init();
    User_Uart2_Init(57600);
    Touch_PinSet();
    GPIO_Test();
    Vol_Set(now_vol);
    RTC_time_init();
    gpio_set_level(LED_GPIO,1);
    ID[0] = Information_Read(&Pernson_Information,conf,ID) + 1;
    AS608_DeleteChar(ID[0],100);
    wifi_init();
    vTaskDelay(pdMS_TO_TICKS(3000));
    uart_write_bytes(SCREEN_UART,page_init,sizeof(page_init));
    vTaskDelay(pdMS_TO_TICKS(1000));
    RTC_Sendtime_main(&now_get,&timeinfo_get);
    
    while(1)
    {   
        flag = Cmd_Judge();
         //打卡界面搜索指纹
        if(flag == 1)
        {
            if(Fingerprint_Search(&SearchResult) == SUCCESS)
            {
                Personal_Information_Search(SearchResult.PageID,&Pernson_Information,now_get,&timeinfo_get,card_clock_config,card_model);
            }
        }
        //录入指纹
        else if(flag == 2)
        {
            if(ID[1] == 0)
            {
                Register_finger_name(ID[0],0x01,Input_name,&Pernson_Information);
                ID[0]++;
            }
            else
            {
                sort_arr(ID);
                Register_finger_name(ID[1],0x01,Input_name,&Pernson_Information);
                uint8_t i = 1;
                while(ID[i] != 0)
                {
                    ID[i] = ID[i + 1];
                    i++;
                }
            } 
        }
        //打卡界面发送时间
        else if(flag == 3)
        {
            RTC_Sendtime_sign(&now_get,&timeinfo_get);
        }
        //系统时间设置
        else if(flag == 4)
        {
            System_clock_config();
        }
        //打卡时间设置
        else if(flag == 5)
        {
            card_model = Card_clock_set(card_clock_config);
        }
        //进入日志界面
        else if(flag == 6)
        {
            vTaskDelay(pdMS_TO_TICKS(200));
            Log_Information_Show(&Pernson_Information,conf,ID);
            //Show_card_clock(&card_clock_config);
        }
        //进入管理员查询指纹界面
        else if(flag == 0x0A)
        {
            Finger_manage(&Pernson_Information,ID,conf,&id_num);
        }
        //设置播报音量
        else if(flag == 0x0D)
        {
            now_vol = User_set_vol(now_vol);
        }
        //进入管理员系统
        else if(flag == 0x10)
        {
            Manager_Finger_Search(&Pernson_Information,&SearchResult);
            RTC_Sendtime_main(&now_get,&timeinfo_get);
        }
       //从任何界面返回主界面
        else if(flag == 0xFF)
        {
            vTaskDelay(pdMS_TO_TICKS(100));   
            RTC_Sendtime_main(&now_get,&timeinfo_get);   
        }
        vTaskDelay(pdMS_TO_TICKS(200));
    }

}
